/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package humanNetwork;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author jamiemacpherson
 */
public class Network {


    protected HashMap<String, Edge> edgeHash = new HashMap<String, Edge>();
    protected HashMap<Integer, Node> nodeHash = new HashMap<Integer, Node>();

    public Collection<Edge> getEdges() {
        return edgeHash.values();
    }

    public Collection<Node> getNodes() {
        return nodeHash.values();
    }

    public HashMap<String, Edge> getEdgeHash() {
        return edgeHash;
    }

    public HashMap<Integer, Node> getNodeHash() {
        return nodeHash;
    }



    public HashSet<Edge> getEdges_asHashSet() {
        HashSet<Edge> set = new HashSet<Edge>();
        set.addAll(edgeHash.values());
        return set;
    }

    public HashSet<Node> getNodes_asHashSet() {
        HashSet<Node> set = new HashSet<Node>();
        set.addAll(nodeHash.values());
        return set;
    }


    public HashMap<String, Edge> getEdges_asHashMap() {
        return edgeHash;
    }

    public HashMap<Integer, Node> getNodes_asHashMap() {
        return nodeHash;
    }

    public void setEdges(Vector<Edge> edges) {
        for(Edge e : edges){
            edgeHash.put(e.getId(), e);
        }
    }

    public void setNodes(Vector<Node> nodes) {
        for(Node n : nodes){
            nodeHash.put(n.getId(), n);
        }
    }

    public Edge addEdge(Edge e){
        Edge myEdge = edgeHash.get(e.getId());

        if(myEdge==null){
            edgeHash.put(e.getId(), e);
            addNode(e.getNode1());
            addNode(e.getNode2());

            return e;
        } else{
            return myEdge;
        }
    }

    public Node addNode(Node n){

        Node myNode = nodeHash.get(n.getId());
        if(myNode==null){
            nodeHash.put(n.getId(), n);
            return n;
        } else{
            return myNode;
        }

    }

    public void setNetwork(File edgeFile) {
        BufferedReader reader = utils.FileUtils.getBufferedReader(edgeFile);
        String line;
        try {
            while ((line = reader.readLine()) != null) {
                String split[] = line.split("\t");
                Node n1 = new Node(Integer.parseInt(split[0]));
                Node n2 = new Node(Integer.parseInt(split[1]));
                Edge e  = new Edge(n1, n2);
//                System.out.println(e.getId());
//                System.out.println(line);


                this.addEdge(e);
            }
        } catch (IOException ex) {
            Logger.getLogger(Network.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            reader.close();
        } catch (IOException ex) {
            Logger.getLogger(Network.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
